﻿package core.ui
{
    import core.enum.*;
    import core.event.*;
    import core.ui.controls.*;
    import flash.display.*;
    import flash.events.*;
    import flash.geom.*;
    import flash.text.*;

    public class NavigatorPanel extends Box
    {
        public var cx:Literal;
        public var cy:Literal;
        public var sx:Literal;
        public var sy:Literal;
        public var zl:NumberInput;
        private var bm:Bitmap;
        private var nw:int;
        private var nh:int;
        private var split:BoxSplit;
        private var slider:Slider;
        private var minus:IconButton;
        private var plus:IconButton;
        private var overview:Sprite;
        private var marker:Sprite;
        private var zoom:Sprite;
        var ratio:Number;
        var canvas:Canvas;
        var tc:Rectangle;

        public function NavigatorPanel(param1:Tuyitu)
        {
            this.tc = new Rectangle();
            super(param1, "rm_navigator", 220, 155);
            this.overview = new Sprite();
            this.overview.graphics.beginFill(9474192);
            this.overview.graphics.drawRect(0, 0, 124, 84);
            this.overview.graphics.beginFill(16777215);
            this.overview.graphics.drawRect(1, 1, 122, 82);
            this.overview.graphics.endFill();
            this.overview.x = 10;
            this.overview.y = 25;
            addChild(this.overview);
            var _loc_2:* = new Bitmap(new CurrCords());
            _loc_2.x = 143;
            _loc_2.y = 37;
            addChild(_loc_2);
            var _loc_3:* = new Literal("X:");
            _loc_3.x = 158;
            _loc_3.y = 28;
            addChild(_loc_3);
            var _loc_4:* = new Literal("Y:");
            _loc_4.x = 158;
            _loc_4.y = 40;
            addChild(_loc_4);
            this.cx = new Literal();
            this.cx.autoSize = TextFieldAutoSize.RIGHT;
            this.cx.x = 200;
            this.cx.y = 28;
            addChild(this.cx);
            this.cy = new Literal();
            this.cy.autoSize = TextFieldAutoSize.RIGHT;
            this.cy.x = 200;
            this.cy.y = 40;
            addChild(this.cy);
            var _loc_5:* = new BoxSplit("small");
            _loc_5.x = 140;
            _loc_5.y = 62;
            addChild(_loc_5);
            var _loc_6:* = new Bitmap(new CurrSelection());
            _loc_6.x = 143;
            _loc_6.y = 75;
            addChild(_loc_6);
            var _loc_7:* = new Literal("W:");
            _loc_7.x = 155;
            _loc_7.y = 66;
            addChild(_loc_7);
            var _loc_8:* = new Literal("H:");
            _loc_8.x = 158;
            _loc_8.y = 78;
            addChild(_loc_8);
            this.sx = new Literal();
            this.sx.autoSize = TextFieldAutoSize.RIGHT;
            this.sx.x = 197;
            this.sx.y = 66;
            addChild(this.sx);
            this.sy = new Literal();
            this.sy.autoSize = TextFieldAutoSize.RIGHT;
            this.sy.x = 197;
            this.sy.y = 78;
            addChild(this.sy);
            this.bm = new Bitmap(null, "auto", true);
            this.bm.x = 2;
            this.bm.y = 2;
            this.overview.addChild(this.bm);
            this.marker = new Sprite();
            this.marker.x = 2;
            this.marker.y = 2;
            this.marker.addEventListener(MouseEvent.MOUSE_DOWN, this.MarkerMouseDown, false, 0, true);
            this.marker.addEventListener(MouseEvent.ROLL_OVER, this.MarkerMouseOver, false, 0, true);
            this.marker.addEventListener(MouseEvent.ROLL_OUT, this.MarkerMouseOut, false, 0, true);
            this.overview.addChild(this.marker);
            this.zoom = new Sprite();
            this.zoom.x = 10;
            this.zoom.y = 123;
            this.split = new BoxSplit("medium");
            this.split.x = 1;
            this.split.y = 0;
            this.zoom.addChild(this.split);
            this.slider = new Slider(100, 30);
            this.slider.addEventListener(SliderEvent.DRAG, this.ZoomDrag, false, 0, true);
            this.slider.x = 17;
            this.slider.y = 4;
            this.zoom.addChild(this.slider);
            this.zl = new NumberInput(36, 100, 5, 800);
            this.zl.t.tabEnabled = false;
            this.zl.x = 151;
            this.zl.y = 5;
            this.zl.addEventListener(NumberEvent.CHANGE, this.SetZoomEvent, false, 0, true);
            this.zoom.addChild(this.zl);
            var _loc_9:* = new Literal("%");
            _loc_9.x = 188;
            _loc_9.y = 6;
            this.zoom.addChild(_loc_9);
            this.minus = new IconButton(param1, "rm_zoom_out", new ZoomMinusOver());
            this.minus.addEventListener(MouseEvent.MOUSE_DOWN, this.ZoomMinus, false, 0, true);
            this.minus.x = -2;
            this.minus.y = 10;
            this.zoom.addChild(this.minus);
            this.plus = new IconButton(param1, "rm_zoom_in", new ZoomPlusOver());
            this.plus.addEventListener(MouseEvent.MOUSE_DOWN, this.ZoomPlus, false, 0, true);
            this.plus.x = 122;
            this.plus.y = 9;
            this.zoom.addChild(this.plus);
            addChild(this.zoom);
            return;
        }// end function

        public function SetZoom(slider:int, slider:Boolean = false, slider:Boolean = false) : void
        {
            if (slider > 800)
            {
                slider = 800;
            }
            else if (slider < 5)
            {
                slider = 5;
            }
            if (this.canvas != null)
            {
                this.canvas.Zoom(slider, slider);
            }
            if (slider)
            {
                this.zl.value = slider;
            }
            if (slider <= 100)
            {
                slider = (slider - 5) / 95 * 30;
            }
            else
            {
                slider = 30 + slider / 800 * 70;
            }
            this.slider.Set(slider / 100);
            ed.tool.Reset();
            return;
        }// end function

        public function GetZoomPercent(SliderEvent:int = -1) : int
        {
            if (SliderEvent == -1)
            {
                SliderEvent = Math.round(this.slider.Get() * 100);
            }
            if (SliderEvent <= 30)
            {
                return SliderEvent / 30 * 95 + 5;
            }
            return (SliderEvent - 30) * 10 + 100;
        }// end function

        public function SetZoomEvent(event:NumberEvent) : void
        {
            this.SetZoom(event.val);
            return;
        }// end function

        public function ZoomPlus(... args) : void
        {
            this.slider.Add(0.05);
            this.SetZoomValue();
            return;
        }// end function

        public function ZoomMinus(... args) : void
        {
            this.slider.Remove(0.05);
            this.SetZoomValue();
            return;
        }// end function

        private function ZoomDrag(event:SliderEvent) : void
        {
            if (!event.manual && Math.round(this.GetZoomPercent(this.slider.Get() * 100)) != this.zl.value)
            {
                this.SetZoomValue();
            }
            return;
        }// end function

        private function SetZoomValue() : void
        {
            var _loc_1:* = this.GetZoomPercent();
            this.zl.value = Math.round(_loc_1);
            if (this.canvas != null)
            {
                this.canvas.Zoom(Math.round(_loc_1));
            }
            ed.tool.Reset();
            return;
        }// end function

        private function MarkerMouseOver(event:MouseEvent) : void
        {
            ed.cursor.Set(ToolType.Hand);
            return;
        }// end function

        private function MarkerMouseOut(event:MouseEvent) : void
        {
            ed.cursor.Set(ToolType.None);
            return;
        }// end function

        private function MarkerMouseDown(event:MouseEvent) : void
        {
            ed.cursor.freeze = true;
            stage.addEventListener(MouseEvent.MOUSE_MOVE, this.MarkerMouseMove, false, 0, true);
            stage.addEventListener(MouseEvent.MOUSE_UP, this.MarkerMouseUp, false, 0, true);
            this.marker.startDrag(false, new Rectangle(2, 2, this.nw - this.marker.width + 1, this.nh - this.marker.height + 1));
            return;
        }// end function

        private function MarkerMouseUp(event:MouseEvent) : void
        {
            stage.removeEventListener(MouseEvent.MOUSE_MOVE, this.MarkerMouseMove, false);
            stage.removeEventListener(MouseEvent.MOUSE_UP, this.MarkerMouseUp, false);
            this.marker.stopDrag();
            ed.cursor.freeze = false;
            if (event.target != this.marker)
            {
                ed.cursor.Set(ToolType.None);
                ed.tool.Reset();
            }
            return;
        }// end function

        function MarkerMouseMove(event:Event) : void
        {
            if (this.canvas != null)
            {
                this.canvas.SetAnchor(new Point(Math.ceil((this.marker.x - 2) / this.ratio), Math.ceil((this.marker.y - 2) / this.ratio)));
            }
            return;
        }// end function

        public function SetReference(slider:Canvas) : void
        {
            this.canvas = slider;
            if (slider != null)
            {
                if (slider.dw / 120 > slider.dh / 90)
                {
                    this.nw = 120;
                    this.nh = Math.round(slider.dh * (this.nw / slider.dw));
                }
                else
                {
                    this.nh = 90;
                    this.nw = Math.round(slider.dw * (this.nh / slider.dh));
                }
                this.overview.graphics.clear();
                this.overview.graphics.beginFill(9474192);
                this.overview.graphics.drawRect(0, 0, this.nw + 4, this.nh + 4);
                this.overview.graphics.beginFill(16777215);
                this.overview.graphics.drawRect(1, 1, this.nw + 2, this.nh + 2);
                this.overview.graphics.beginBitmapFill(new Transparent());
                this.overview.graphics.drawRect(2, 2, this.nw, this.nh);
                this.overview.graphics.endFill();
                this.ratio = (this.nw + this.nh) / (slider.dw + slider.dh);
                this.bm.bitmapData = slider.screenshot;
                this.bm.width = this.nw;
                this.bm.height = this.nh;
                this.RedrawMarker();
                this.SetZoom(slider.zoomlevel * 100, true, true);
                if (slider.selection != null)
                {
                    this.SetSelectionSize(slider.selection.width, slider.selection.height);
                }
                else
                {
                    this.SetSelectionSize(0, 0);
                }
            }
            else
            {
                this.bm.bitmapData = null;
                this.overview.graphics.clear();
                this.overview.graphics.beginFill(9474192);
                this.overview.graphics.drawRect(0, 0, 124, 84);
                this.overview.graphics.beginFill(16777215);
                this.overview.graphics.drawRect(1, 1, 122, 82);
                this.overview.graphics.endFill();
                this.marker.graphics.clear();
                this.SetSelectionSize(0, 0);
            }
            return;
        }// end function

        public function RedrawMarker() : void
        {
            this.tc.x = Math.round(this.canvas.clip.x * this.ratio);
            this.tc.y = Math.round(this.canvas.clip.y * this.ratio);
            this.tc.width = this.canvas.canvaswidth > this.canvas.masking.width ? (this.nw) : (Math.round(this.canvas.clip.width * this.ratio));
            this.tc.height = this.canvas.canvasheight > this.canvas.masking.height ? (this.nh) : (Math.round(this.canvas.clip.height * this.ratio));
            this.marker.x = this.tc.x + 2;
            this.marker.y = this.tc.y + 2;
            if (this.tc.width != 0 && this.tc.height != 0)
            {
                if (this.marker.width != this.tc.width || this.marker.height != this.tc.height)
                {
                    this.marker.graphics.clear();
                    this.marker.graphics.beginFill(16777215, 0.25);
                    this.marker.graphics.drawRect(0, 0, this.tc.width, this.tc.height);
                    this.marker.graphics.endFill();
                    this.marker.graphics.lineStyle(1, 16711680, 1, true);
                    this.marker.graphics.moveTo(0, 0);
                    this.marker.graphics.lineTo((this.tc.width - 1), 0);
                    this.marker.graphics.lineTo((this.tc.width - 1), (this.tc.height - 1));
                    this.marker.graphics.lineTo(0, (this.tc.height - 1));
                    this.marker.graphics.lineTo(0, 0);
                }
            }
            return;
        }// end function

        public function SetSelectionSize(slider:int, slider:int) : void
        {
            if (slider != 0 && slider != 0)
            {
                this.sx.text = slider.toString();
                this.sy.text = slider.toString();
            }
            else
            {
                var _loc_3:String = "";
                this.sy.text = "";
                this.sx.text = _loc_3;
            }
            return;
        }// end function

    }
}
